// Include this file into reader_read_items.php, body section

var timer = 0;
var timeout = 1500;
var timeInt = null;

var json_index = 0;
var json_obj = null;

var dTitle = $('.new-items > .item-head > .item-title');
var dTime = $('.new-items > .item-head > .item-time');
var dAuthor = $('.new-items > .item-head > .item-author');
var dContent = $('.new-items > .item-content');

var param = '';

function init()
{
	timer = 0;
	timeout = 1500;
	json_index = 0;
}

function startTimer()
{
	if(json_obj == null)
	{
		getNewItems();
	}
	
	timeInt = setInterval(function() 
		{
			showNewItems();
			show_message_number();
			
			if(incrementIndex())
			{				
				json_obj = getNewItems();
			}
		}
		, timeout
	);
}

function pauseTimer()
{
	clearInterval(timeInt);
	
	var index = 0;
	
	if(json_index == 0)
	{
		index = json_obj.length - 1;
	}
	else
	{
		index = json_index - 1;
	}

	$.get
	(
	 	"reader_operation.php?action=set_read_item&item_id=" + json_obj[index].id, 
		function(data)
		{
			var old_title = dTime.text();
			dTime.text('(already read) ' + old_title);
		}
	);
}

function stopTimer()
{
	clearInterval(timeInt);
	init();
}

/**
 * Show new items to users
 */
function showNewItems()
{
	var title = '';
	var date = '';
	var author = '';
	var cTitle = '';
	var description = '';
	var len = 0;
	
	if(json_obj == null)
	{
		title = 'No new item now.';
		date = 'No new item now.';
		author = '';
		cTitle = 'No new item now.';
		description = 'No new item now.';
		len = 0;
	}
	else
	{
		title = htmlspecialchars_decode(json_obj[json_index].title);
		date = json_obj[json_index].pubdate;
		author = json_obj[json_index].author;
		cTitle = json_obj[json_index].channel_title;
		description = htmlspecialchars_decode(json_obj[json_index].description);
		len = json_obj.length;
	}
	
	dTitle.html(title + ' (' + (json_index + 1) + '/' + len + ')');
	dTime.text(date);
	dAuthor.text(cTitle + ' by ' + author);
	dContent.html(description);
}

function setParameter(username, key, cid)
{	
	param = 'username=' + username;
	param += '&keyword=' + key;
	param += '&channel_id=' + cid;
	
	return param;
}

/**
 * Fetch the new items by specified parameter using AJAX
 */
function getNewItems()
{
	$.get
	(
	 	"reader_operation.php?action=show_update_item&" + param, 
		function(data)
		{
			json_obj = eval('(' + data + ')');
		}
	);
}

/**
 * Advance the index number of JSON object by one step.
 * Return true if the index number exceeds the maximum value.
 */
function incrementIndex()
{
	if(json_obj == null)
	{
		return true;
	}
	
	if(++json_index >= json_obj.length)
	{
		json_index = 0;
		
		return true;
	}
	
	return false;
}

function htmlspecialchars_decode(p_string)
{
	p_string = p_string.replace(/&amp;/g, '&');
	p_string = p_string.replace(/&lt;/g, '<');
	p_string = p_string.replace(/&gt;/g, '>');
	p_string = p_string.replace(/&quot;/g, '"');
	p_string = p_string.replace(/&#039;/g, '\'');
	
	return p_string;
};

function show_message_number()
{
	$.get
	(
	 	"reader_operation.php?action=message_number&" + param, 
		function(data)
		{
			$('#number_msg').text(data);
		}
	);
}

$('.new-items').click(function()
	{
		if(json_obj != null)
		{
			var item_content = $(this).children('.item-content');

			if(item_content.css('display') == 'none')
			{
				pauseTimer();			 
				show_message_number();
				$(this).children('.item-content').show(500);
				$(this).children('.item-author').show(500);
				$('#new-alert').attr('style', 'display: none');
			}
			else if(item_content.css('display') == 'block')
			{
				startTimer();
				$(this).children('.item-content').hide(500);
				$(this).children('.item-author').hide(500);
				$('#new-alert').attr('style', 'display: inline');
			}
		}
	}
);